﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using jSLApp.Client.ApplicationBase;
using jSLApp.Client.Workflow;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;

namespace jSLApp.Client.Forms
{
    public partial class Chat : DialogControl
    {
        public Chat()
        {
            InitializeComponent();

            Loaded += (sender, args) =>
                          {
                              if (ChatController.Current != null) ChatController.Current.StartTimer();
                          };
            Unloaded += (o, eventArgs) =>
                            {
                                if (ChatController.Current != null) ChatController.Current.UpdateTimer();
                            };

        }

        


        private void gotoUser_Clicked(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;
            if (element.Tag as string != null) 
                MainContentContainer.Current.GotoUserAbout((string) element.Tag);
            else
            {
                InfoBox.ShowDialog("Что бы отправить сообщение - введите текст и нажмите Enter");
            }
        }

        private void send_Clicked(object sender, RoutedEventArgs e)
        {
            Send();
        }

        private void Send()
        {
            if(!string.IsNullOrEmpty(txt.Text))
            {
                DoWork(false,(o, args) => Communication.Proxy.CreateChat(args.String),(sender1, eventArgs) =>
                                                                                          {
                                                                                              txt.Text = string.Empty;
                                                                                          },txt.Text);
            }
        }

        private void key_up(object sender, KeyEventArgs e)
        {
            if(e.Key==Key.Enter)
            {
                Send();
                e.Handled = true;
            }
        }

        protected override void AcceptButtonClicked()
        {
            Send();
        }

        public override bool IsOkVisible
        {
            get { return false; }
        }

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override object CancelContent
        {
            get { return "Закрыть"; }
        }

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }
    }
}
